include(ExternalProject)

macro(get_git_branch _git_branch)
    find_package(Git QUIET)
    if(GIT_FOUND)
      execute_process(
        COMMAND ${GIT_EXECUTABLE} symbolic-ref -q HEAD
        OUTPUT_VARIABLE ${_git_branch}
        OUTPUT_STRIP_TRAILING_WHITESPACE
        ERROR_QUIET
        WORKING_DIRECTORY
          ${CMAKE_CURRENT_SOURCE_DIR}
        )
    endif()
endmacro()

macro(build_subdirectory_innel dir)
  if (NOT PRODUCT_BUILD_DIR)
    set(PRODUCT_BUILD_DIR ${ROOT_DIR}/out/${PRODUCT_NAME})
  endif()

  get_filename_component(target ${dir} NAME)
  if (EXISTS ${ROOT_DIR}/${dir}/include)
    include_directories(${ROOT_DIR}/${dir}/include)
  endif()
  if (EXISTS ${PRODUCT_BUILD_DIR}/${dir}/include)
    include_directories(${PRODUCT_BUILD_DIR}/${dir}/include)
  endif()
  if (EXISTS ${PRODUCT_BUILD_DIR}/${dir}/lib)
    link_directories(${PRODUCT_BUILD_DIR}/${dir}/lib)
  endif()
  # message("include_directories: ${PRODUCT_BUILD_DIR}/${dir}")

  # list(APPEND CMAKE_FIND_ROOT_PATH "${PRODUCT_BUILD_DIR}/${dir}")
  # list(APPEND CMAKE_MODULE_PATH "${PRODUCT_BUILD_DIR}/${dir}/cmake")
  # list(APPEND CMAKE_PREFIX_PATH "${PRODUCT_BUILD_DIR}/${dir}/cmake")

  # 将输入路径转换为大写并替换斜线为下划线
  string(TOUPPER "${dir}" UPPER_PATH)
  string(REPLACE "/" "_" CUR_MACRO_PATH "${UPPER_PATH}")

  message("CUR_MACRO_PATH: ${CUR_MACRO_PATH}")
  get_property(IS_ADDED_ALREADY GLOBAL PROPERTY ${CUR_MACRO_PATH} SET)

  message("IS_ADDED_ALREADY: ${IS_ADDED_ALREADY}")
  if(NOT IS_ADDED_ALREADY)
    add_subdirectory(${ROOT_DIR}/${dir} ${ROOT_DIR}/out/${PRODUCT_NAME}/${dir})
    set_property(GLOBAL APPEND PROPERTY ${CUR_MACRO_PATH} 1)
  endif()
endmacro()

function(build_subdirectory)
  list(GET ARGV 0 dir)
  message("PRODUCT_BUILD_DIR: ${PRODUCT_BUILD_DIR}")
  set(current_mode "")
  # 遍历所有传递给函数的参数
  list(REMOVE_AT ARGV 0)
  foreach(arg ${ARGV})
    # 检查是否遇到了模式切换关键字
    if(arg STREQUAL "INCLUDE")
      set(current_mode "INCLUDE")
    elseif(arg STREQUAL "LINK")
      set(current_mode "LINK")
    elseif(arg STREQUAL "DEFINE")
      set(current_mode "DEFINE")
    else()
      # 如果当前有模式，并且不是关键字，则添加到相应的列表
      if(current_mode STREQUAL "INCLUDE")
        include_directories(${PRODUCT_ROOT_DIR}/${arg})
        include_directories(${PRODUCT_BUILD_DIR}/${arg})
      elseif(current_mode STREQUAL "LINK")
        link_directories(${PRODUCT_BUILD_DIR}/${arg})
      elseif(current_mode STREQUAL "DEFINE")
        add_definitions(-D${arg})
      endif()
    endif()
  endforeach()
  build_subdirectory_innel(${dir})
endfunction()


    # GIT_REPOSITORY ${url}
    # GIT_TAG  ${tag}
function(build_project)
  list(GET ARGV 0 dir)
  if (NOT PRODUCT_BUILD_DIR)
    set(PRODUCT_BUILD_DIR ${ROOT_DIR}/out/${PRODUCT_NAME})
  endif()

  get_filename_component(target ${dir} NAME)
  message("target: ${target}")
  ExternalProject_Add(${target}
    SOURCE_DIR ${ROOT_DIR}/${dir}
    BINARY_DIR ${PRODUCT_BUILD_DIR}/${dir}
    INSTALL_DIR ${PRODUCT_INSTALL_DIR}/${dir}
    CMAKE_ARGS -DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE}
              -DCMAKE_INSTALL_PREFIX={CMAKE_INSTALL_PREFIX}
              -DCMAKE_BUILD_TYPE={CMAKE_BUILD_TYPE}
  )
endfunction(build_project)

function(auto_strip_target target)
    # 获取目标输出路径和文件名
    get_target_property(target_type ${target} TYPE)
    if (target_type STREQUAL "EXECUTABLE" OR target_type MATCHES "LIBRARY")
        add_custom_command(TARGET ${target} POST_BUILD
            COMMAND ${CMAKE_STRIP} --strip-all $<TARGET_FILE:${target}>
            COMMENT "Stripping symbols for target: ${target}"
        )
    endif()
endfunction()

# set find executable programs on the host system path
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
